package com.ruoyi.project.cardbox.request.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cardbox.request.domain.OrderRequest;
import com.ruoyi.project.cardbox.request.service.IOrderRequestService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 请购单Controller
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@Controller
@RequestMapping("/cardbox/request")
public class OrderRequestController extends BaseController
{
    private String prefix = "cardbox/request";

    @Autowired
    private IOrderRequestService orderRequestService;

    @RequiresPermissions("cardbox:request:view")
    @GetMapping()
    public String request()
    {
        return prefix + "/request";
    }

    /**
     * 查询请购单列表
     */
    @RequiresPermissions("cardbox:request:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderRequest orderRequest)
    {
        startPage();
        List<OrderRequest> list = orderRequestService.selectOrderRequestList(orderRequest);
        return getDataTable(list);
    }

    /**
     * 导出请购单列表
     */
    @RequiresPermissions("cardbox:request:export")
    @Log(title = "请购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderRequest orderRequest)
    {
        List<OrderRequest> list = orderRequestService.selectOrderRequestList(orderRequest);
        ExcelUtil<OrderRequest> util = new ExcelUtil<OrderRequest>(OrderRequest.class);
        List<String> footer = Arrays.asList(
                " 说明: ",
                " 按规定的附件配置生产；",
                " 纸板平整，不弯曲或折叠；",
                " 压紧到位，不隐藏包。"
        );

        return util.exportExcelWithFooter(list, "请购单数据", "请购单数据", footer);
//        return util.exportExcel(list, "请购单数据");
    }
    /**
     * 导出选中请购单列表
     */
    @RequiresPermissions("cardbox:request:export")
    @Log(title = "请购单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelected")
    @ResponseBody
    public AjaxResult exportSelected(@RequestParam("ids") List<String> ids) {
        List<OrderRequest> list = orderRequestService.selectOrderRequestByIds(ids);
        ExcelUtil<OrderRequest> util = new ExcelUtil<>(OrderRequest.class);
        List<String> footer = Arrays.asList(
                " 说明: ",
                " 按规定的附件配置生产；",
                " 纸板平整，不弯曲或折叠；",
                " 压紧到位，不隐藏包。"
        );

        return util.exportExcelWithFooter(list, "请购单数据", "请购单数据", footer);
//        return util.exportExcel(list, "请购单数据");
    }





//    /**
//     * 新增请购单
//     */
//    @RequiresPermissions("cardbox:request:add")
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }
//
//    /**
//     * 新增保存请购单
//     */
//    @RequiresPermissions("cardbox:request:add")
//    @Log(title = "请购单", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(OrderRequest orderRequest)
//    {
//        return toAjax(orderRequestService.insertOrderRequest(orderRequest));
//    }

    /**
     * 修改请购单
     */
    @RequiresPermissions("cardbox:request:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderRequest orderRequest = orderRequestService.selectOrderRequestById(id);
        mmap.put("orderRequest", orderRequest);
        return prefix + "/edit";
    }

    /**
     * 修改保存请购单
     */
    @RequiresPermissions("cardbox:request:edit")
    @Log(title = "请购单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderRequest orderRequest)
    {
        return toAjax(orderRequestService.updateOrderRequest(orderRequest));
    }

    /**
     * 删除请购单
     */
    @RequiresPermissions("cardbox:request:remove")
    @Log(title = "请购单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderRequestService.deleteOrderRequestByIds(ids));
    }
}
